package com.cskaoyan.controller.admin;

import com.cskaoyan.bean.entity.Storage;
import com.cskaoyan.bean.vo.BasePageData;
import com.cskaoyan.bean.vo.BaseParam;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.StorageCommonVo;
import com.cskaoyan.service.StorageService;
import com.cskaoyan.utils.BaseRespVoUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;

@RestController
@RequestMapping("admin/storage")
public class StorageController {

    @Resource
    StorageService storageService;


    @RequestMapping("list")
    public BaseRespVo list(String key, String name, BaseParam param) {
        BasePageData data = storageService.query(key, name, param);

        return BaseRespVo.ok(data);
    }

    /**
     * 添加 对象存储
     */
    @PostMapping("create")
    public BaseRespVo create(MultipartFile file) throws IOException {
        StorageCommonVo storageCommonVo = storageService.create(file);
        return BaseRespVo.ok(storageCommonVo);
    }
    /**
     * 修改 对象存储
     */
    @PostMapping("update")
    public BaseRespVo update(@RequestBody Storage storage) {
        StorageCommonVo storageCommonVo = storageService.update(storage);
        return BaseRespVo.ok(storageCommonVo);
    }
    /**
     * 删除 对象存储
     */
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody Storage storage) {
        int i = storageService.delete(storage.getId());
        return BaseRespVoUtil.respByAffectedRow(i);
    }
}
